using System;
using System.Data;
using System.Data.SqlClient;
using DBUtility;

namespace DAL
{
    /// <summary>
    /// 接口ILandInfo的实现
    /// </summary>
    public class LandInfo : IDAL.ILandInfo
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            string strSql = "select count(*) from LandInfo where Enabled=1 and LandID = " + Id.ToString();
            return DBHelperSQL.GetResult(strSql).ToString().Trim() == "1" ? true : false;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string name)
        {
            string strSql = "select count(*) from LandInfo where Enabled=1 and LandName = '" + name.Trim() + "'";
            return DBHelperSQL.GetResult(strSql).ToString().Trim() == "1" ? true : false;
        }

        /// <summary>
        /// 增加一条记录
        /// </summary>
        public bool Add(Model.LandInfo model)
        {
            string strSql = "insert into LandInfo(ATTRIBUTEID, LANDNAME, LANDSCOPE, ENABLED) values(@AttributeID, @LandName, @LandScope, @Enabled)";
            SqlParameter AttributeID = new SqlParameter("ATTRIBUTEID", SqlDbType.Int); AttributeID.Value = model.AttributeID;
            SqlParameter LandName = new SqlParameter("LANDNAME", SqlDbType.NVarChar); LandName.Value = model.LandName;
            SqlParameter LandScope = new SqlParameter("LANDSCOPE", SqlDbType.NVarChar); LandScope.Value = model.LandScope;
            SqlParameter Enabled = new SqlParameter("ENABLED", SqlDbType.Bit); Enabled.Value = model.Enabled;
            return DBHelperSQL.GetNums(strSql, new SqlParameter[] { AttributeID, LandName, LandScope, Enabled }) == 1 ? true : false;
        }

        /// <summary>
        /// 增加一条记录，返回ID
        /// </summary>
        public int Add2(Model.LandInfo model)
        {
            string strSql = "insert into LandInfo(ATTRIBUTEID, LANDNAME, LANDSCOPE, ENABLED) values(@AttributeID, @LandName, @LandScope, @Enabled) SELECT @@IDENTITY AS returnName;";
            SqlParameter AttributeID = new SqlParameter("ATTRIBUTEID", SqlDbType.Int); AttributeID.Value = model.AttributeID;
            SqlParameter LandName = new SqlParameter("LANDNAME", SqlDbType.NVarChar); LandName.Value = model.LandName;
            SqlParameter LandScope = new SqlParameter("LANDSCOPE", SqlDbType.NVarChar); LandScope.Value = model.LandScope;
            SqlParameter Enabled = new SqlParameter("ENABLED", SqlDbType.Bit); Enabled.Value = model.Enabled;
            return DBHelperSQL.GetInsertID(strSql, new SqlParameter[] { AttributeID, LandName, LandScope, Enabled });
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        public bool Update(Model.LandInfo model)
        {
            string strSql = "update LandInfo set ATTRIBUTEID = @AttributeID, LANDNAME = @LandName, LANDSCOPE = @LandScope, ENABLED = @Enabled where LandID= " + model.LandID.ToString();
            SqlParameter AttributeID = new SqlParameter("ATTRIBUTEID", SqlDbType.Int); AttributeID.Value = model.AttributeID;
            SqlParameter LandName = new SqlParameter("LANDNAME", SqlDbType.NVarChar); LandName.Value = model.LandName;
            SqlParameter LandScope = new SqlParameter("LANDSCOPE", SqlDbType.NVarChar); LandScope.Value = model.LandScope;
            SqlParameter Enabled = new SqlParameter("ENABLED", SqlDbType.Bit); Enabled.Value = model.Enabled;
            return DBHelperSQL.GetNums(strSql, new SqlParameter[] { AttributeID, LandName, LandScope, Enabled }) == 1 ? true : false;
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        public bool Delete(int Id)
        {
            string strSql = "delete from LandInfo where LandID = " + Id.ToString();
            return DBHelperSQL.GetResult(strSql).ToString().Trim() == "1" ? true : false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.LandInfo GetModel(int Id)
        {
            string strSql = "select * from LandInfo where LandID = " + Id.ToString();
            Model.LandInfo model = new Model.LandInfo();
            DataSet ds = DBHelperSQL.GetDataSet(strSql);
            model.LandID = Id;
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.LandID = Convert.ToInt32(ds.Tables[0].Rows[0]["LandID"]);
                model.AttributeID = Convert.ToInt32(ds.Tables[0].Rows[0]["AttributeID"]);
                model.LandName = Convert.ToString(ds.Tables[0].Rows[0]["LandName"]);
                model.LandScope = Convert.ToString(ds.Tables[0].Rows[0]["LandScope"]);
                model.Enabled = Convert.ToBoolean(ds.Tables[0].Rows[0]["Enabled"]);
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.LandInfo GetModel(string name)
        {
            //string strSql = "select * from LandInfo where LandName = '" + name.Trim() + "'";
            //Model.LandInfo model = new Model.LandInfo();
            //DataSet ds = DBHelperSQL.GetDataSet(strSql);
            //if (ds.Tables[0].Rows.Count > 0)
            //{
                //model.LandID = Convert.ToInt32(ds.Tables[0].Rows[0]["LandID"]);
                //model.AttributeID = Convert.ToInt32(ds.Tables[0].Rows[0]["AttributeID"]);
                //model.LandName = Convert.ToString(ds.Tables[0].Rows[0]["LandName"]);
                //model.LandScope = Convert.ToString(ds.Tables[0].Rows[0]["LandScope"]);
                //model.Enabled = Convert.ToBoolean(ds.Tables[0].Rows[0]["Enabled"]);
                //return model;
            //}
            //else
            //{
                //return null;
            //}
            return null;
        }

        /// <summary>
        /// 获取数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            string strSql = "select * from LandInfo where Enabled=1 ";
            if (strWhere.Trim() != "")
                strSql += "and " + strWhere;
            return DBHelperSQL.GetDataSet(strSql);
        }
    }
}
